﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference path="AjaxAction.js" />

function saveCommitment(url)
{
    var title = document.getElementById("title");
    var createdBy = document.getElementById("createdBy");
    var verification = document.getElementById("verification");
    var spent = document.getElementById("spent");
    var left = document.getElementById("left");

    if (title.value == "")
    {
        alert("must insert a title");
        return;
    }
    if (verification.value == "")
    {
        alert("must insert a verification");
        return;
    }
    if (createdBy.value == "")
    {
        alert("must insert a createdBy name");
        return;
    }
    if (spent.value == "")
    {
        alert("must insert a spent value");
        return;
    }            
    if (left.value == "")
    {
        alert("must insert a left value");
        return;
    }
    
  
    var options = {url: url, update: 'standup' };
    Sys.UI.DomElement.removeCssClass($get("progressBar"), "off");
    new HttpHelper.AjaxAction().invoke(options, HttpHelper.encodeFormElements($get('commitmentEditContainer')), onRequestFinished);    
    clearCommitment();
}

function onRequestFinished(executor, eventArgs) {
    Sys.UI.DomElement.addCssClass($get("progressBar"), "off");
}

function deleteCommitment(url)
{
    var options = {url: url, update: 'standup' };
    Sys.UI.DomElement.removeCssClass($get("progressBar"), "off");
    new HttpHelper.AjaxAction().invoke(options, HttpHelper.encodeFormElements($get('commitmentEditContainer')), onRequestFinished);
    clearCommitment();
}

function editCommitment(tr) 
{
    var id = getStringValue(getElementsByClassName('id', 'input', tr)[0]);
    var title = getStringValue(getElementsByClassName('title', 'input', tr)[0]);
    var createdBy = getStringValue(getElementsByClassName('createdBy', 'input', tr)[0]);
    var verification = getStringValue(getElementsByClassName('verification', 'input', tr)[0]);
    var spent = getStringValue(getElementsByClassName('spent', 'input', tr)[0]);
    var left = getStringValue(getElementsByClassName('left', 'input', tr)[0]);
    
    var formElements;
    var container = $get('commitmentEditContainer');
    $get('id', container).value = id;
    $get('title', container).value = title;
    $get('createdBy', container).value = createdBy;
    $get('verification', container).value = verification;
    $get('spent', container).value = spent;
    $get('left', container).value = left;
}

function clearCommitment() 
{
    var container = $get('commitmentEditContainer');
    $get('id', container).value = '';
    $get('title', container).value = '';
    $get('verification', container).value = '';
    $get('spent', container).value = '0';
    $get('left', container).value = '';
}

function getStringValue(el) {
    if (el.tagName == "INPUT") {
        return trim(el.value);
    } else {
        return trim(el.firstChild.nodeValue);
    }
}

// helper functions
function trim(str) {
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
function findParentNode(el, nodeType) {
    nodeType = nodeType.toUpperCase();
    while(el != null) {
        el = el.parentNode;
        if(el.nodeName.toUpperCase() == nodeType) break;
    }
    return el;
}

function getElementsByClassName(strClass, strTag, objContElm) {
  strTag = strTag || "*";
  objContElm = objContElm || document;
  var objColl = objContElm.getElementsByTagName(strTag);
  if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
  var arr = new Array();
  var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
  var arrClass = strClass.split(delim);
  for (var i = 0, j = objColl.length; i < j; i++) {
    var arrObjClass = objColl[i].className.split(' ');
    if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
    var c = 0;
    comparisonLoop:
    for (var k = 0, l = arrObjClass.length; k < l; k++) {
      for (var m = 0, n = arrClass.length; m < n; m++) {
        if (arrClass[m] == arrObjClass[k]) c++;
        if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
          arr.push(objColl[i]);
          break comparisonLoop;
        }
      }
    }
  }
  return arr;
}

